﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BaseText;
using Srvtools;

public partial class WorkTime_WorkTimeEvection : System.Web.UI.Page
{

    #region 系统

    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitializeComponent();
            Master.DataSource = WMaster;

            MasterPage_MasterPage mpg = (MasterPage_MasterPage)this.Page.Master;
            mpg.HelpHref = "~/Help/WorkTime/员工出差流程.htm";
            mpg.PageName = "出差单";
            mpg.PicUrl = "";
            mpg.IntiBar();
        }
    }

    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WorkTime_WorkTimeEvection));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = false;
        this.WMaster.AlwaysClose = true;
        this.WMaster.DeleteIncomplete = true;
        this.WMaster.Guid = null;
        this.WMaster.LastKeyValues = null;
        this.WMaster.Locale = new System.Globalization.CultureInfo("zh-CN");
        this.WMaster.PacketRecords = 100;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SWorkTime.WorkTime_Evection";
        this.WMaster.ServerModify = true;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();

    }

    #endregion

    #region 事件

    protected void WebFormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        TextBox startDate = (TextBox)this.WebFormView1.FindControl("ZLTextBox1");
        TextBox endDate = (TextBox)this.WebFormView1.FindControl("ZLTextBox2");

        if (startDate == null || endDate == null)
            return;

        int hours = this.GetHours(Convert.ToDateTime(startDate.Text), Convert.ToDateTime(endDate.Text));
        e.Values["EveHours"] = hours;

    }

    protected void WebFormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        ZLTextBox startDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox1");
        ZLTextBox endDate = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox2");

        if (startDate == null || endDate == null)
            return;

        int hours = this.GetHours(startDate.GetDateTime, endDate.GetDateTime);
        e.NewValues["EveHours"] = hours;
    }

    protected void btnHours_Click(object sender, EventArgs e)
    {
        ZLTextBox tbHours = (ZLTextBox)this.WebFormView1.FindControl("ZLTextBox3");
        TextBox startDate = (TextBox)this.WebFormView1.FindControl("ZLTextBox1");
        TextBox endDate = (TextBox)this.WebFormView1.FindControl("ZLTextBox2");
        if (tbHours == null || startDate == null || endDate == null)
            return;
        if (this.ValidateDate(Convert.ToDateTime(startDate.Text), Convert.ToDateTime(endDate.Text)) == false)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('终止日期必须大于起始日期\\r\\n或是\\r\\n请假时间不在工作时间之内');", true);
            return;
        }
        tbHours.Text = this.GetHours(Convert.ToDateTime(startDate.Text), Convert.ToDateTime(endDate.Text)).ToString();
    }

    #endregion

    #region 验证

    private DateTime start = DateTime.MinValue;
    private DateTime end = DateTime.MinValue;

    protected bool ValidateDateFrom(object value)
    {
        start = DateTime.Parse(value.ToString());
        return this.ValidateDate(start, end);
    }

    protected bool ValidateDateTo(object value)
    {
        end = DateTime.Parse(value.ToString());
        return this.ValidateDate(start, end);
    }

    private bool ValidateDate(DateTime start, DateTime end)
    {
        if (start != DateTime.MinValue && end != DateTime.MinValue)
        {
            int sHour = start.Hour;
            int sMinute = start.Minute;
            int eHour = end.Hour;
            int eMinute = end.Minute;

            if (sHour < 8 || sHour == 8 && sMinute < 30 || sHour > 17 || eHour < 8 || eHour == 8 && eMinute < 30 || eHour > 17)
            {
                return false;
            }

            if (start.CompareTo(end) >= 0)
            {
                return false;
            }
        }
        return true;
    }

    #endregion

    #region 自定义方法

    private int GetHours(DateTime stDate, DateTime end)
    {
        int counHour = 0;

        int sYear = stDate.Year;
        int sMonth = stDate.Month;
        int sDay = stDate.Day;
        int sHour = stDate.Hour;

        int eYear = end.Year;
        int eMonth = end.Month;
        int eDay = end.Day;
        int eHour = end.Hour;

        if (sYear == eYear && sMonth == eMonth && sDay == eDay)
        {
            if (sHour >= 12 || eHour <= 12)
            {
                counHour += 4;
            }
            else
            {
                counHour += 8;
            }
        }
        else
        {
            TimeSpan ts = end - stDate;
            if (ts.Days > 1)
            {
                counHour += (ts.Days - 1) * 8;
            }
            if (sHour >= 12)
                counHour += 4;
            else
                counHour += 8;
            if (eHour <= 12)
                counHour += 4;
            else
                counHour += 8;
        }
        return counHour;
    }

    protected bool IsDepVisible(string dep)
    {
        return dep != string.Empty;
    }



    #endregion

    #region 默认值绑定

    protected object GetDateTo()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 0, 0);
    }

    protected object GetDateFrom()
    {
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 8, 30, 0);
    }

    protected object GetEmployeeId()
    {
        String str = Srvtools.CliUtils.GetValue("_UserCode")[1].ToString();
        str = int.Parse(str).ToString();
        return str;
    }

    #endregion


    protected void WebFormView1_DataBound(object sender, EventArgs e)
    {
        if (WebFormView1.CurrentMode == FormViewMode.Insert || WebFormView1.CurrentMode == FormViewMode.Edit)
        {
            if (Request.Params["MS"] != null)
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = false;
            }
            else
            {
                WebRefVal tbEmployee = (WebRefVal)this.WebFormView1.FindControl("WebRefVal1");
                if (tbEmployee != null)
                    tbEmployee.ReadOnly = true;
            }

        }
    }
}
